package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.mapper.SysMenuMapper;
import com.itheima.ydd.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

//    @Override
//    public SysMenu getChildren(SysMenu sysMenu) {
//        return null;
//    }
//
//    @Override
//    public SysMenu getUseChildren(SysMenu sysMenu, List<SysRoleMenu> sysRoleMenus) {
//        return null;
//    }

    /**
     * 递归查询所有菜单
     * @param menuDto id=0
     * @param ids   菜单id集合
     * @return
     */
    @Override
    public List<MenuDto> selectAll(MenuDto menuDto,List<Integer> ids) {
        //根据传过来的菜单id,找到所有子类菜单
        LambdaQueryWrapper<SysMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysMenu::getPid,menuDto.getId());
        lqw.in(ids!=null,SysMenu::getId,ids);
        List<SysMenu> menuList = this.list(lqw);
        //如果查不到,从出口出来
        if (menuList.isEmpty()) {
            return null;
        }
        List<MenuDto> menuDtos = menuList.stream().map(menu -> {
            MenuDto md = new MenuDto();
            BeanUtils.copyProperties(menu, md);
            //递归查询
            List<MenuDto> childrnMenu = this.selectAll(md, ids);
            if (childrnMenu!=null){
                //能查到才赋值
                md.setChildren(childrnMenu);
            }
            return md;
        }).collect(Collectors.toList());
        return menuDtos;
    }



}
